package com.seisys.controller.system.mac;


import java.sql.Timestamp;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.seisys.controller.base.BaseController;
import com.seisys.entity.system.User;
import com.seisys.service.system.LogService;
import com.seisys.service.system.mac.MacService;
import com.seisys.util.IData;
import com.seisys.util.LogUtil;


/**
 * 类名称：MacController 创建人：pxn1 创建时间：2017年6月12日
 * 
 * @version
 */
@Controller
@RequestMapping(value = "/mac")
public class MacController extends BaseController
{

    private static final String MODULENAME = "mac地址管理";

    @Autowired
    private MacService macService;

    @Resource(name = "logService")
    private LogService logService;

    @RequestMapping(value = "/goMac")
    public ModelAndView goMac()
        throws Exception
    {
        ModelAndView mv = this.getModelAndView();
        mv.setViewName("system/mac/mac_list");
        return mv;
    }

    /**
     * 列表
     */
    @RequestMapping("/list")
    @ResponseBody
    public Object list()
        throws Exception
    {
        Map<String, Object> result = new HashMap<String, Object>();
        IData param = IData.getInstance(request);
        List<IData> list = macService.showListAllMac(param);
        result.put("rows", list);
        result.put("total", param.getPage().getTotalResult());
        return result;
    }

    /**
     * 新增页面
     * 
     * @throws Exception
     */
    @SuppressWarnings("unchecked")
    @RequestMapping(value = "/toAdd")
    public ModelAndView toAdd()
        throws Exception
    {
        ModelAndView mv = this.getModelAndView();
        IData param = IData.getInstance(request);
        IData bo = new IData();
        if (param.get("mac_addr_code") != null)
        {
            bo = macService.showMacIData(param);
        }
        String flag = "";
        if (bo.get("MAC_ADDR_CODE") != null)
        {
            flag = "update";
        }
        else
        {
            flag = "add";
        }
        bo.put("flag", flag);

        mv.addObject("mac", bo);
        mv.setViewName("system/mac/mac_add");
        return mv;
    }

    /**
     * 批量删除
     * 
     * @throws Exception
     */
    @RequestMapping(value = "/deleteMac")
    @ResponseBody
    public Object deleteMac()
        throws Exception
    {
        Map<String, Object> map = new HashMap<String, Object>();

        IData param = IData.getInstance(request);
        String mac_addr_codes = param.get("mac_addr_codes").toString();

        if (null != mac_addr_codes && !"".equals(mac_addr_codes))
        {
            String ArrayProductIds[] = mac_addr_codes.split(",");
            // 批量删除
            macService.delMac(ArrayProductIds);
            LogUtil.getInstance().operationLogger(LogUtil.DELETE, MODULENAME, mac_addr_codes,
                logService, request);
        }

        return map;
    }

    @SuppressWarnings("unchecked")
    @RequestMapping(value = "/saveMac")
    @ResponseBody
    public Object saveMac()
        throws Exception
    {
        Map<String, Object> result = new HashMap<String, Object>();
        try
        {
            String userName = ((User)request.getSession().getAttribute("sessionUser")).getNAME();
            Timestamp dateNow = new Timestamp(System.currentTimeMillis());
            IData param = IData.getInstance(request);
            String flag = param.get("flag").toString();

            if ("update".equals(flag))
            {
                param.put("update_user", userName);
                param.put("update_time", dateNow);
                macService.updateMac(param);
                LogUtil.getInstance().operationLogger(LogUtil.UPDATE, MODULENAME,
                    param.get("mac_addr_code").toString(), logService, request);

            }
            else
            {
                param.put("create_user", userName);
                param.put("create_time", dateNow);
                param.put("update_user", userName);
                param.put("update_time", dateNow);
                macService.saveMac(param);
                LogUtil.getInstance().operationLogger(LogUtil.ADD, MODULENAME,
                    param.get("mac_addr_code").toString(), logService, request);
            }
            result.put("isSuccess", true);
        }
        catch (Exception e)
        {
            result.put("isSuccess", false);
        }
        return result;
    }

    /**
     * Description: (验证mac地址名称是否已存在)
     * 
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/hasCode")
    public Object hasCode(String mac_addr_code)
    {
        Map<String, Object> result = new HashMap<>();

        try
        {
            List<IData> vo = macService.hasCode(mac_addr_code);
            if (vo != null && vo.size() != 0)
            {
                result.put("msg", "ok");
            }

        }
        catch (Exception e)
        {
            result.put("msg", "error");
            throw new RuntimeException("验证mac地址名称是否已存在出错!", e);
        }

        return result;
    }

}
